package com.buildings.activity;

import android.Manifest;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.util.TimeUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.DPoint;
import com.buildings.R;
import com.buildings.activity.table.GeophySvyPointActivity;
import com.buildings.activity.table.VolcanicSamplePointActivity;
import com.buildings.api.Api;
import com.buildings.api.Constant;
import com.buildings.base.BaseActivity;
import com.buildings.base.BaseApplication;
import com.buildings.bean.CreaterDbEntity;
import com.buildings.bean.DrillHoleDbEntity;
import com.buildings.bean.FaultSvyPointDbEntity;
import com.buildings.bean.ForDataInfoEntity;
import com.buildings.bean.GeoGeomorphySvyPointDbEntity;
import com.buildings.bean.GeochemicalSvyLineDbEntity;
import com.buildings.bean.GeochemicalSvyPointDbEntity;
import com.buildings.bean.GeologicalSvyLineDbEntitiy;
import com.buildings.bean.GeologicalSvyPlanningLineDbEntity;
import com.buildings.bean.GeologicalSvyPlanningPtDbEntity;
import com.buildings.bean.GeologicalSvyPointDbEntity;
import com.buildings.bean.GeomorStationDbEntity;
import com.buildings.bean.GeomorphySvyLineDbEntity;
import com.buildings.bean.GeomorphySvyPointDbEntity;
import com.buildings.bean.GeomorphySvyReProfDbEntity;
import com.buildings.bean.GeomorphySvyRegionDbEntity;
import com.buildings.bean.GeomorphySvySamplePointDbEntity;
import com.buildings.bean.GeophySvyLineDbEntity;
import com.buildings.bean.GeophySvyPointDbEntity;
import com.buildings.bean.LavaDbEntity;
import com.buildings.bean.LocationEntity;
import com.buildings.bean.LoginBean;
import com.buildings.bean.SamplePointDbEntity;
import com.buildings.bean.SeeTableBean;
import com.buildings.bean.StratigraphySvyPointDbEntity;
import com.buildings.bean.TableBean;
import com.buildings.bean.TableLocatinoBean;
import com.buildings.bean.TanCaodianDbEntity;
import com.buildings.bean.UserInfoBean;
import com.buildings.bean.VolcanicSamplePointDbEntity;
import com.buildings.bean.VolcanicSamplePointEntity;
import com.buildings.bean.VolcanicSvyPointDbEntity;
import com.buildings.event.EventCode;
import com.buildings.event.LocationEvent;
import com.buildings.event.MessageEvent;
import com.buildings.event.MessageEventEntity;
import com.buildings.http.BaseCallBack;
import com.buildings.http.JsonBuilder;
import com.buildings.http.MyHttpClient;
import com.buildings.map.ArcGisUtil;
import com.buildings.map.GoogleMapLayer;
import com.buildings.map.TianDiMapUtils;
import com.buildings.util.CustomToast;
import com.buildings.util.DataUtils;
import com.buildings.util.GPSUtil;
import com.buildings.util.HYPermissionUtil;
import com.buildings.util.SPUtil;
import com.buildings.util.Utils;
import com.buildings.view.FragmentDialog;
import com.buildings.view.LocationSubmitFragmentDialog;
import com.buildings.view.SelectMapLayerFragmentDialog;
import com.buildings.view.StartTaskFragmentDialog;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.ShapefileFeatureTable;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.layers.WebTiledLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.view.BackgroundGrid;
import com.esri.arcgisruntime.mapping.view.Callout;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.IdentifyGraphicsOverlayResult;
import com.esri.arcgisruntime.mapping.view.LocationDisplay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.MarkerSymbol;
import com.esri.arcgisruntime.symbology.PictureMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class MainMapActivity extends BaseActivity implements AMapLocationListener {
    @BindView(R.id.mapview)
    MapView mapView;
    @BindView(R.id.iv_location)
    ImageView ivLocation;
    @BindView(R.id.tv_is_on_line)
    TextView tvIsOnLine;
    @BindView(R.id.iv_check)
    ImageView ivCheck;
    @BindView(R.id.tv_translte)
    TextView tvTranslte;
    @BindView(R.id.tv_project)
    TextView tvProject;
    @BindView(R.id.tv_location1)
    TextView tvLocation1;
    private ArcGisUtil arcGisUtil;
    private ArcGISMap map;
    private WebTiledLayer vectorBaseMapAnno;
    private WebTiledLayer vectorBaseMap;
    private Point locationPoint;
    private int i = 1;
    // 是否在线 true 在线模式 false 离线模式
    private boolean isOnLine = true;
    List<WebTiledLayer> vectorBaseMapLayerList = new ArrayList<>();
    List<WebTiledLayer> imageBaseMapLayerList = new ArrayList<>();
    List<WebTiledLayer> gogleMapList = new ArrayList<>();
    private Dialog myDialog;
    private LogoutReceiver logoutReceiver;
    private LocationEntity locationEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_map);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        getPermission();
        initMap();
        initAndroidLocation();
        logoutReceiver = new LogoutReceiver();
        getUserInfo((String) SPUtil.get("userId", ""));
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("action.logout");
        registerReceiver(logoutReceiver, intentFilter);
        initLocation(3);
    }

    private void getPermission() {
        HYPermissionUtil.checkPermissions(this, granted -> {
            if (!granted) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getPointsOrDrawableMark();
    }

    private WebTiledLayer imageBaseMap;
    private WebTiledLayer imageBaseMapAnno;
//    private WebTiledLayer webTiledLayer;

    public void initMap() {
        arcGisUtil = new ArcGisUtil(this, mapView);
        map = new ArcGISMap();

        vectorBaseMap = TianDiMapUtils.getTianDiMap(TianDiMapUtils.MapType.URL_CN_VECTORBASEMAP);
        vectorBaseMapAnno = TianDiMapUtils.getTianDiMap(TianDiMapUtils.MapType.URL_CN_VECTORBASEMAP_ANNOTATION);
//        vectorBaseMapAnno = TianDiMapUtils.getTianDiMap(TianDiMapUtils.MapType.URL_CN_GOOGLE_MAP);

        vectorBaseMapLayerList.add(vectorBaseMap);
        vectorBaseMapLayerList.add(vectorBaseMapAnno);

        imageBaseMap = TianDiMapUtils.getTianDiMap(TianDiMapUtils.MapType.URL_CN_IMAGEBASEMAP);
        imageBaseMapAnno = TianDiMapUtils.getTianDiMap(TianDiMapUtils.MapType.URL_CN_IMAGEBASEMAP_ANNOTATION);
//        webTiledLayer = GoogleMapLayer.CreateGoogleLayer(GoogleMapLayer.MapType.VECTOR);

        imageBaseMapLayerList.add(imageBaseMap);
        imageBaseMapLayerList.add(imageBaseMapAnno);
//        gogleMapList.add(webTiledLayer);
        map.setMaxScale(TianDiMapUtils.getSCALES()[TianDiMapUtils.getSCALES().length - 1]);
        mapView.setMap(map);
        mapView.setAttributionTextVisible(false);
        mapView.setBackgroundGrid(new BackgroundGrid(Color.WHITE, Color.WHITE, 0, 2f));
        map.getOperationalLayers().addAll(vectorBaseMapLayerList);

        vectorBaseMap.loadAsync();
        vectorBaseMapAnno.loadAsync();

        mapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mapView) {

            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                android.graphics.Point point = new android.graphics.Point((int) e.getX(), (int) e.getY());
                Point p = mapView.screenToLocation(point);
                Point mapPoint = (Point) GeometryEngine.project(p, SpatialReference.create(4326));
                final ListenableFuture<List<IdentifyGraphicsOverlayResult>> listListenableFuture
                        = mapView.identifyGraphicsOverlaysAsync(point, 12, false, 1000);
                Callout callout = arcGisUtil.showPop(MainMapActivity.this, null, p, isOnLine);
                if (null != callout) {
                    callout.dismiss();
                }
                if (isTranslte) {
                    if (isOnLine) {
                        StartTaskFragmentDialog.newInstance(mapPoint.getX(), mapPoint.getY(), isOnLine).show(getSupportFragmentManager(), "");
                    } else {
                        StartTaskFragmentDialog.newInstance(mapPoint.getX(), mapPoint.getY(), isOnLine).show(getSupportFragmentManager(), "");
                    }
                } else {
                    listListenableFuture.addDoneListener(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                List<IdentifyGraphicsOverlayResult> grOverlayResult = listListenableFuture.get();
                                for (IdentifyGraphicsOverlayResult identifyGraphicsOverlayResult : grOverlayResult) {
                                    arcGisUtil.showPop(MainMapActivity.this, identifyGraphicsOverlayResult.getGraphics(), p, isOnLine).show();
                                }
                            } catch (ExecutionException executionException) {
                                executionException.printStackTrace();
                            } catch (InterruptedException interruptedException) {
                                interruptedException.printStackTrace();
                            }
                        }
                    });
                }
                return true;
            }

            @Override
            public boolean onRotate(MotionEvent event, double rotationAngle) {
                return false;
            }

        });

    }

    private long time1 = 0;

    public void initAndroidLocation() {
        LocationDisplay locationDisplayManager = mapView.getLocationDisplay();//获取定位类
        locationDisplayManager.setAutoPanMode(LocationDisplay.AutoPanMode.NAVIGATION);
        locationDisplayManager.setShowLocation(true);
        locationDisplayManager.addLocationChangedListener(new LocationDisplay.LocationChangedListener() {
            @Override
            public void onLocationChanged(LocationDisplay.LocationChangedEvent locationChangedEvent) {
                locationPoint = locationChangedEvent.getLocation().getPosition();
                if (locationPoint == null) {
                    return;
                }
                if (i == 1) {
                    double[] arr = GPSUtil.gps84_To_Gcj02(locationPoint.getX(), locationPoint.getY());
                    moveMapCenter(locationPoint);
                    i++;
                }
            }
        });
        locationDisplayManager.startAsync();//开始定位
        getTableDataAndDrawMark();
    }

    /**
     * 获取表单和打点
     */
    private void getTableDataAndDrawMark() {

    }

    private void moveMapCenter(Point point) {
        mapView.setViewpointCenterAsync(point, TianDiMapUtils.getSCALES()[16]);
    }

    @OnClick({R.id.iv_location, R.id.tv_is_on_line, R.id.tv_project, R.id.iv_check, R.id.tv_translte
            , R.id.iv_setting, R.id.ll_data_collection})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_location:
                if (null != locationPoint) {
                    moveMapCenter(locationPoint);
                }
                break;
            case R.id.tv_is_on_line:
                isOnLine = !isOnLine;
                if (isOnLine) {
                    tvIsOnLine.setText("离线模式");
                    tvProject.setText("项目列表");
                    Constant.IS_ON_LINE = 1;
                } else {
                    tvIsOnLine.setText("退出离线模式");
                    tvProject.setText("项目离线表单");
                    Constant.IS_ON_LINE = 0;
                }
                break;
            case R.id.tv_project:
                if (isOnLine) {
                    startActivity(new Intent(activity, ProjectListActivity.class));
                } else {
                    startActivity(new Intent(activity, OutLineActivity.class));
                }
                break;
            case R.id.iv_check:
                SelectMapLayerFragmentDialog.newInstance().show(getSupportFragmentManager(), "");
                break;
            case R.id.tv_translte:
                isTranslte = !isTranslte;
                if (isTranslte) {
                    tvTranslte.setText("退出采集");
                } else {
                    tvTranslte.setText("开始采集");
                }
                break;
            case R.id.iv_setting:
                startActivity(new Intent(this, SettingActivity.class));
//                List<LocationEntity> geologicalSvyPointDbEntities =
//                        BaseApplication.getInstance().getDaoSession().loadAll(LocationEntity.class);
//                Environment.getDownloadCacheDirectory()
//                File file = new File((null), "/ploygun.shp");
//                if (file.exists()) {
//                    showShp(file.getPath());
//                } else {
//                }
                break;
            case R.id.ll_data_collection:
                String text = tvLocation1.getText().toString().trim();
                if ("轨迹".equals(text)) {
                    tvLocation1.setText("结束");
                    if (isOnLine) {
                        LocationSubmitFragmentDialog.newInstance(1).show(getSupportFragmentManager(), "");
                    } else {
                        LocationSubmitFragmentDialog.newInstance(2).show(getSupportFragmentManager(), "");
                    }
                } else {
                    tvLocation1.setText("轨迹");
                    if (isOnLine) {
                        //上传轨迹
                        List<LocationEntity> locationEntities =
                                BaseApplication.getInstance().getDaoSession().loadAll(LocationEntity.class);
                        if (locationEntities.size() == 0) {
                            return;
                        }
                        LocationEntity locationEntity1 = locationEntities.get(locationEntities.size() - 1);
                        locationEntity1.setEndTime(DataUtils.longTodata(System.currentTimeMillis()));
                        submitLocation(locationEntity1);
                        //上传最后一条 然后本地删除
                    } else {
                        //结束采集
                        try {
                            locationEntity.setEndTime(DataUtils.longTodata(System.currentTimeMillis()));
                            BaseApplication.getInstance().getDaoSession().insertOrReplace(locationEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }
                break;
        }
    }

    private void submitLocation(LocationEntity locationEntity1) {
        JsonBuilder jsonBuilder = new JsonBuilder(this);
        jsonBuilder.put("startTime", locationEntity1.getStartTime());
        jsonBuilder.put("lonlatArray", locationEntity1.getPoint());
        jsonBuilder.put("endTime", locationEntity1.getEndTime());
        jsonBuilder.put("taskId", locationEntity1.getTaskId());
        jsonBuilder.put("userId", locationEntity1.getUserId());
        jsonBuilder.put("createUser", locationEntity1.getUserId());
        MyHttpClient
                .newBuilder()
                .post()
                .url(Api.HDDC_CJ_HISTORY_POINTS)
                .addParam(jsonBuilder.toJsonString())
                .build()
                .enqueue(new BaseCallBack<LoginBean>() {
                    @Override
                    public void onSuccess(LoginBean bean) {
                        super.onSuccess(bean);
                        if (bean.getCode() == 200) {
                            try {
                                BaseApplication.getInstance().getDaoSession().delete(locationEntity1);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            CustomToast.showToast(MainMapActivity.this, "提交成功");
                        }
                        if (myDialog != null)
                            myDialog.dismiss();
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        if (myDialog != null)
                        CustomToast.showToast(MainMapActivity.this, "网络异常");
                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(MainMapActivity.this, "网络异常");
                    }
                });
    }

    private boolean isTranslte = false;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (logoutReceiver != null) {
            unregisterReceiver(logoutReceiver);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveMessage(MessageEventEntity messageEventEntity) {
        switch (messageEventEntity.getCode()) {
            case EventCode.SELECT_MAP_LAYER1:
                map.getOperationalLayers().clear();
                map.getOperationalLayers().addAll(imageBaseMapLayerList);
                break;
            case EventCode.SELECT_MAP_LAYER2:
                map.getOperationalLayers().clear();
                map.getOperationalLayers().addAll(vectorBaseMapLayerList);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveMesage(LocationEvent messageEventEntity) {
        locationEntity = new LocationEntity();
        locationEntity.setStartTime(DataUtils.longTodata(System.currentTimeMillis()));
        locationEntity.setProjectId(messageEventEntity.getProjectId());
        locationEntity.setTaskId(messageEventEntity.getTaskId());
        locationEntity.setUserId((String) SPUtil.get("userId", ""));
        initLocation(messageEventEntity.getSecond());
    }

    private void getUserInfo(String userId) {
        MyHttpClient
                .newBuilder()
                .get()
                .url(Api.USER_INFO + "?userId=" + userId)
                .build()
                .enqueue(new BaseCallBack<UserInfoBean>() {
                    @Override
                    public void onSuccess(UserInfoBean bean) {
                        super.onSuccess(bean);
                        if (bean != null && bean.getCode() == 200) {
                            if (bean.getData() != null) {
                                if (!TextUtils.isEmpty(bean.getData().getUserName())) {
                                    SPUtil.save("userName", bean.getData().getUserName());
                                }
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        CustomToast.showToast(activity, "网络异常");
                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
                        CustomToast.showToast(activity, "网络异常");
                    }
                });
    }

    class LogoutReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Utils.logout(activity);
            finish();
        }
    }

    public AMapLocationClient mLocationClient = null;

    public void initLocation(int i) {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //初始化AMapLocationClientOption对象
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        //设置定位场景
        mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.Sport);
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
        mLocationOption.setInterval(i * 1000);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(false);
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        //gcj_02转wgs84
        double[] arr = GPSUtil.gcj02_To_Gps84(aMapLocation.getLatitude(), aMapLocation.getLongitude());
        Point point = new Point(arr[1], arr[0], SpatialReference.create(4326));
        if (i == 1) {
            moveMapCenter(point);
            i++;
        }
        if (tvLocation1.getText().toString().trim().equals("轨迹")) {
            return;
        }
        if (null != locationEntity) {
            if (TextUtils.isEmpty(locationEntity.getPoint())) {
                locationEntity.setPoint(arr[1] + "," + arr[0]);
            } else {
                locationEntity.setPoint(locationEntity.getPoint() + ";" + arr[1] + "," + arr[0]);
            }
        }
        BaseApplication.getInstance().getDaoSession().insertOrReplace(locationEntity);
    }

    private void getPointsOrDrawableMark() {
        String url = Api.FIND_APP_FORM_DATA_INFO + "?userId="
                + SPUtil.get("userId", "");
        MyHttpClient
                .newBuilder()
                .get()
                .url(url)
                .build()
                .enqueue(new BaseCallBack<ForDataInfoEntity>() {
                    @Override
                    public void onSuccess(ForDataInfoEntity bean) {
                        super.onSuccess(bean);
                        if (bean != null && bean.getData() != null && bean.getData().size() != 0) {
                            List<TableLocatinoBean> allTableLocationList = new ArrayList<>();
                            //本地的加保存另一种图，点击进入可编辑
                            getLocalData(allTableLocationList);
                            allTableLocationList.addAll(bean.getData());
                            drawJH(MainMapActivity.this, mapView, allTableLocationList);
                        }
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        MainMapActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                CustomToast.showToast(MainMapActivity.this, "网络异常");
                            }
                        });
                    }
                });
    }

    private void getLocalData(List<TableLocatinoBean> allTableLocationList) {
        List<GeologicalSvyPlanningLineDbEntity> students = BaseApplication.getInstance().getDaoSession().loadAll(GeologicalSvyPlanningLineDbEntity.class);
        if (students.size() != 0) {
            for (GeologicalSvyPlanningLineDbEntity geologicalSvyPlanningLineDbEntity : students) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("1");
                tableLocatinoBean.setUuid(geologicalSvyPlanningLineDbEntity.getUuid());
                tableLocatinoBean.setLat(geologicalSvyPlanningLineDbEntity.getLat());
                tableLocatinoBean.setLon(geologicalSvyPlanningLineDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                tableLocatinoBean.set_id(geologicalSvyPlanningLineDbEntity.get_id());
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeologicalSvyPlanningPtDbEntity> geologicalSvyPlanningPtDbEntities = BaseApplication.getInstance().getDaoSession().loadAll(GeologicalSvyPlanningPtDbEntity.class);
        if (geologicalSvyPlanningPtDbEntities.size() != 0) {
            for (GeologicalSvyPlanningPtDbEntity geologicalSvyPlanningPtDbEntity : geologicalSvyPlanningPtDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("2");
                tableLocatinoBean.setUuid(geologicalSvyPlanningPtDbEntity.getUuid());
                tableLocatinoBean.setLat(geologicalSvyPlanningPtDbEntity.getLat());
                tableLocatinoBean.setLon(geologicalSvyPlanningPtDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                tableLocatinoBean.set_id(geologicalSvyPlanningPtDbEntity.get_id());
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<FaultSvyPointDbEntity> faultSvyPointDbEntities = BaseApplication.getInstance().getDaoSession().loadAll(FaultSvyPointDbEntity.class);
        if (faultSvyPointDbEntities.size() != 0) {
            for (FaultSvyPointDbEntity faultSvyPointDbEntity : faultSvyPointDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("3");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeoGeomorphySvyPointDbEntity> geoGeomorphySvyPointDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(GeoGeomorphySvyPointDbEntity.class);
        if (geoGeomorphySvyPointDbEntities.size() != 0) {
            for (GeoGeomorphySvyPointDbEntity faultSvyPointDbEntity : geoGeomorphySvyPointDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("4");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeologicalSvyLineDbEntitiy> geologicalSvyLineDbEntitiys
                = BaseApplication.getInstance().getDaoSession().loadAll(GeologicalSvyLineDbEntitiy.class);
        if (geologicalSvyLineDbEntitiys.size() != 0) {
            for (GeologicalSvyLineDbEntitiy faultSvyPointDbEntity : geologicalSvyLineDbEntitiys) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("5");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeologicalSvyPointDbEntity> geologicalSvyPointDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(GeologicalSvyPointDbEntity.class);
        if (geologicalSvyPointDbEntities.size() != 0) {
            for (GeologicalSvyPointDbEntity faultSvyPointDbEntity : geologicalSvyPointDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("6");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<StratigraphySvyPointDbEntity> stratigraphySvyPointDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(StratigraphySvyPointDbEntity.class);
        if (stratigraphySvyPointDbEntities.size() != 0) {
            for (StratigraphySvyPointDbEntity faultSvyPointDbEntity : stratigraphySvyPointDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("7");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<TanCaodianDbEntity> tanCaodianDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(TanCaodianDbEntity.class);
        if (tanCaodianDbEntities.size() != 0) {
            for (TanCaodianDbEntity faultSvyPointDbEntity : tanCaodianDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("8");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeomorphySvyLineDbEntity> geomorphySvyLineDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(GeomorphySvyLineDbEntity.class);
        if (geomorphySvyLineDbEntities.size() != 0) {
            for (GeomorphySvyLineDbEntity faultSvyPointDbEntity : geomorphySvyLineDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("9");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeomorphySvyPointDbEntity> geomorphySvyPointDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(GeomorphySvyPointDbEntity.class);
        if (geomorphySvyPointDbEntities.size() != 0) {
            for (GeomorphySvyPointDbEntity faultSvyPointDbEntity : geomorphySvyPointDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("10");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeomorphySvyRegionDbEntity> geomorphySvyRegionDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(GeomorphySvyRegionDbEntity.class);
        if (geomorphySvyRegionDbEntities.size() != 0) {
            for (GeomorphySvyRegionDbEntity faultSvyPointDbEntity : geomorphySvyRegionDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("11");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeomorphySvyReProfDbEntity> geomorphySvyReProfDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(GeomorphySvyReProfDbEntity.class);
        if (geomorphySvyReProfDbEntities.size() != 0) {
            for (GeomorphySvyReProfDbEntity faultSvyPointDbEntity : geomorphySvyReProfDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("12");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeomorphySvySamplePointDbEntity> geomorphySvySamplePointDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(GeomorphySvySamplePointDbEntity.class);
        if (geomorphySvySamplePointDbEntities.size() != 0) {
            for (GeomorphySvySamplePointDbEntity faultSvyPointDbEntity : geomorphySvySamplePointDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("13");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeomorStationDbEntity> geomorStationDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(GeomorStationDbEntity.class);
        if (geomorStationDbEntities.size() != 0) {
            for (GeomorStationDbEntity faultSvyPointDbEntity : geomorStationDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("14");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<DrillHoleDbEntity> drillHoleDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(DrillHoleDbEntity.class);
        if (drillHoleDbEntities.size() != 0) {
            for (DrillHoleDbEntity faultSvyPointDbEntity : drillHoleDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("15");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<SamplePointDbEntity> samplePointDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(SamplePointDbEntity.class);
        if (samplePointDbEntities.size() != 0) {
            for (SamplePointDbEntity faultSvyPointDbEntity : samplePointDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("16");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeophySvyLineDbEntity> geophySvyLineDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(GeophySvyLineDbEntity.class);
        if (geophySvyLineDbEntities.size() != 0) {
            for (GeophySvyLineDbEntity faultSvyPointDbEntity : geophySvyLineDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("17");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeophySvyPointDbEntity> geophySvyPointDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(GeophySvyPointDbEntity.class);
        if (geophySvyPointDbEntities.size() != 0) {
            for (GeophySvyPointDbEntity faultSvyPointDbEntity : geophySvyPointDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("18");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeochemicalSvyLineDbEntity> geochemicalSvyLineDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(GeochemicalSvyLineDbEntity.class);
        if (geochemicalSvyLineDbEntities.size() != 0) {
            for (GeochemicalSvyLineDbEntity faultSvyPointDbEntity : geochemicalSvyLineDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("19");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<GeochemicalSvyPointDbEntity> geochemicalSvyPointDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(GeochemicalSvyPointDbEntity.class);
        if (geochemicalSvyPointDbEntities.size() != 0) {
            for (GeochemicalSvyPointDbEntity faultSvyPointDbEntity : geochemicalSvyPointDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("20");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<CreaterDbEntity> createrDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(CreaterDbEntity.class);
        if (createrDbEntities.size() != 0) {
            for (CreaterDbEntity faultSvyPointDbEntity : createrDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("21");
                tableLocatinoBean.setUuid(faultSvyPointDbEntity.getUuid());
                tableLocatinoBean.set_id(faultSvyPointDbEntity.get_id());
                tableLocatinoBean.setLat(faultSvyPointDbEntity.getLat());
                tableLocatinoBean.setLon(faultSvyPointDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<LavaDbEntity> lavaDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(LavaDbEntity.class);
        if (lavaDbEntities.size() != 0) {
            for (LavaDbEntity lavaDbEntity : lavaDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("22");
                tableLocatinoBean.setUuid(lavaDbEntity.getUuid());
                tableLocatinoBean.set_id(lavaDbEntity.get_id());
                tableLocatinoBean.setLat(lavaDbEntity.getLat());
                tableLocatinoBean.setLon(lavaDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<VolcanicSamplePointDbEntity> volcanicSamplePointDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(VolcanicSamplePointDbEntity.class);
        if (volcanicSamplePointDbEntities.size() != 0) {
            for (VolcanicSamplePointDbEntity lavaDbEntity : volcanicSamplePointDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("23");
                tableLocatinoBean.setUuid(lavaDbEntity.getUuid());
                tableLocatinoBean.set_id(lavaDbEntity.get_id());
                tableLocatinoBean.setLat(lavaDbEntity.getLat());
                tableLocatinoBean.setLon(lavaDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
        List<VolcanicSvyPointDbEntity> volcanicSvyPointDbEntities
                = BaseApplication.getInstance().getDaoSession().loadAll(VolcanicSvyPointDbEntity.class);
        if (volcanicSvyPointDbEntities.size() != 0) {
            for (VolcanicSvyPointDbEntity lavaDbEntity : volcanicSvyPointDbEntities) {
                TableLocatinoBean tableLocatinoBean = new TableLocatinoBean();
                tableLocatinoBean.setType("24");
                tableLocatinoBean.setUuid(lavaDbEntity.getUuid());
                tableLocatinoBean.setLat(lavaDbEntity.getLat());
                tableLocatinoBean.set_id(lavaDbEntity.get_id());
                tableLocatinoBean.setLon(lavaDbEntity.getLon());
                tableLocatinoBean.setLine(2);
                allTableLocationList.add(tableLocatinoBean);
            }
        }
    }

    public static HashMap<Class, GraphicsOverlay> drawJHLayerMap = new HashMap<>();
    public static HashMap<Class, List<Graphic>> drawJHGraphicMap = new HashMap<>();

    /**
     * 绘制聚合点数据
     *
     * @param context
     * @param mapView
     * @param jhEntities
     */
    public static void drawJH(Context context, MapView mapView, List<TableLocatinoBean> jhEntities) {
        if (drawJHLayerMap.get(context.getClass()) == null) {
            GraphicsOverlay layer = new GraphicsOverlay();
            mapView.getGraphicsOverlays().add(layer);
            drawJHLayerMap.put(context.getClass(), layer);
        }
        drawJHLayerMap.get(context.getClass()).getGraphics().clear();
        if (jhEntities == null || jhEntities.size() == 0) {
            return;
        }
        List<Graphic> graphics = new ArrayList<>();
        for (int i = 0; i < jhEntities.size(); i++) {
            TableLocatinoBean bean = jhEntities.get(i);
            if (TextUtils.isEmpty(bean.getLat()) || TextUtils.isEmpty(bean.getLon())) {
                continue;
            }
            double lat = Double.parseDouble(bean.getLat());
            double lon = Double.parseDouble(bean.getLon());

            Point point = new Point(lat, lon, SpatialReference.create(4326));
            PictureMarkerSymbol symbolImage = null;
            if (bean.isLine() == 1){
                symbolImage = new PictureMarkerSymbol((BitmapDrawable) context.getResources().getDrawable(R.drawable.icon_mark));
            }else {
                symbolImage = new PictureMarkerSymbol((BitmapDrawable) context.getResources().getDrawable(R.drawable.icon_local));
            }
            Map<String, Object> attributes = new HashMap<>();
            attributes.put("lat", point.getY());
            attributes.put("lon", point.getX());
            attributes.put("uuid", bean.getUuid());
            attributes.put("type", bean.getType());
            attributes.put("isLine", bean.isLine());
            if (bean.get_id() != -1){
                attributes.put("_id", bean.get_id());
            }
            Graphic graphicStart = new Graphic(point, attributes, symbolImage);
            graphics.add(graphicStart);
        }
        drawJHGraphicMap.put(context.getClass(), graphics);
        drawJHLayerMap.get(context.getClass()).getGraphics().addAll(graphics);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.pause();
    }
}
